<template>
  <div id="app">
    <el-container class="home-container">
      <!-- 头部 -->
      <el-header>
        <div>
          <img src="../assets/logo.png" alt />
          <span>电商后台管理系统</span>
        </div>
        <el-dropdown trigger="click">
          <span class="el-dropdown-link">
            用户名
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <div>
                <i class="iconfont icon-renxiang"></i>
                <span>个人中心</span>
              </div>
            </el-dropdown-item>
            <el-dropdown-item>
              <div @click="logout">
                <i class="iconfont icon-tuichuleft"></i>
                <span>退出系统</span>
              </div>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <!-- 主体 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse?'64px':'235px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <!-- 侧边栏菜单 -->
          <el-menu
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath"
            text-color="#5a5a5a"
            active-text-color="#448ef7"
          >
            <!-- 一级菜单 -->
            <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>

              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/home/'+subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="saveNavState('/home/'+subItem.path)"
              >
                <template slot="title">
                  <!-- 文本 -->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容 -->
        <el-main>
          <!-- 路由占位 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //导航菜单渲染
      menulist: [
        {
          id: 1,
          authName: "概况",
          children: [
            {
              id: 1,
              authName: "实时概况",
              path: "realTime",
            },
          ],
        },
        {
          id: 2,
          authName: "会员管理",
          children: [
            {
              id: 2,
              authName: "会员查询",
              path: "userQuery",
            },
            {
              id: 3,
              authName: "会员等级",
              path: "userLevel",
            },
            {
              id: 4,
              authName: "付费会员卡",
              path: "clubCard",
            },
            {
              id: 5,
              authName: "会员标签",
              path: "/5",
            },
            {
              id: 6,
              authName: "会员权益",
              path: "/6",
            },
            {
              id: 7,
              authName: "积分规则",
              path: "/7",
            },
            {
              id: 8,
              authName: "成长值规则",
              path: "/8",
            },
            {
              id: 9,
              authName: "储值规则",
              path: "/9",
            },
          ],
        },
        {
          id: 3,
          authName: "订单管理",
          children: [
            {
              id: 10,
              authName: "订单查询",
              path: "order",
            },
          ],
        },
        {
          id: 4,
          authName: "营销",
          children: [
            {
              id: 12,
              authName: "优惠卷",
              path: "coupons",
            },
            {
              id: 13,
              authName: "限时折扣",
              path: "/13",
            },
            {
              id: 14,
              authName: "满减满折",
              path: "/14",
            },
            {
              id: 15,
              authName: "秒杀",
              path: "/15",
            },
            {
              id: 16,
              authName: "满包邮",
              path: "/16",
            },
            {
              id: 17,
              authName: "满赠",
              path: "/17",
            },
          ],
        },
        {
          id: 5,
          authName: "数据分析",
          children: [
            {
              id: 18,
              authName: "ProfileData",
              path: "/Profile data",
            },
            {
              id: 19,
              authName: "流量分析",
              path: "/19",
            },
            {
              id: 20,
              authName: "交易分析",
              path: "/20",
            },
            {
              id: 21,
              authName: "会员分析",
              path: "/21",
            },
          ],
        },
        {
          id: 6,
          authName: "设置",
          children: [
            {
              id: 22,
              authName: "店铺信息",
              path: "/22",
            },
            {
              id: 23,
              authName: "个人信息",
              path: "/23",
            },
          ],
        },
      ],
      iconsObj: {
        1: "iconfont icon-yingxiao",
        2: "iconfont icon-huiyuan",
        3: "iconfont icon-guanli",
        4: "iconfont icon-yingxiao1",
        5: "iconfont icon-shujufenxi",
        6: "iconfont icon-shezhi",
      },
      //菜单是否折叠
      isCollapse: false,
      //激活的链接地址
      activePath: "",
    };
  },
  created() {
    /* this.getMenulikt(); */
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    //退出账号
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取菜单
    /* async getMenulikt() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status == 200) {
        this.menulist = res.data;
      } else {
        return this.$message.error(res.meta.msg);
      }
    }, */
    //点击按钮，切换菜单的折叠展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //保存导航按钮激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" spcoed>
#app,
.home-container {
  height: 100%;
}

.el-header {
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #448ef7;
  font-size: 19px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 10px;
    }

    img {
      height: 43px;
      width: 43px;
    }
  }
}

.el-aside {
  background-color: #fff;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #f0f2f5;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #eee;
  font-size: 10px;
  line-height: 24px;
  color: #448ef7;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-dropdown {
  cursor: pointer;
}
</style>